package com.sojson.util.security.shiro.config.filter;

import java.util.ArrayList;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.DelegatingFilterProxy;

import com.sojson.util.filter.FilterUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 配置过滤器bean
 * 
 * @author liu
 * @date 2020-11-02
 */
@Slf4j
// @Configuration
public class FilterConfig {

    /**
     * 注入shiro过滤器,没有这个有些功能会报错
     * 
     * @return
     */
    @Bean
    public FilterRegistrationBean<DelegatingFilterProxy> shiro() {
        FilterRegistrationBean<DelegatingFilterProxy> filterRegistrationBean =
            new FilterRegistrationBean<>(new DelegatingFilterProxy());
        // 过滤路径
        filterRegistrationBean.setUrlPatterns(getPathPatterns(FilterUtil.FILTER));
        // 有一个作用就是shiro要找的filter的名字
        filterRegistrationBean.setName("customShiroFilterFactoryBean");
        // 设置过滤器的执行顺序0最高
        filterRegistrationBean.setOrder(1);
        log.info("shiro过滤器注入成功！");
        return filterRegistrationBean;
    }

    /**
     * 
     * @param type 哪种过滤器
     * @return
     */
    private ArrayList<String> getPathPatterns(int type) {
        return FilterUtil.getPathPatterns(type);
    }

}